package me.elvis.calendarview;


import me.elvis.library.Calendar;
import me.elvis.library.RangeMonthView;
import ohos.agp.components.AttrHelper;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.agp.utils.Rect;
import ohos.app.Context;

/**
 * 范围选择月视图
 * Created by huanghaibin on 2018/9/13.
 */

public class CustomRangeMonthView extends RangeMonthView {

    private int mRadius;
    private Rect mRect = new Rect();

    public CustomRangeMonthView(Context context) {
        super(context);
    }


    @Override
    protected void onPreviewHook() {
        mRadius = Math.min(mItemWidth, mItemHeight) / 5 * 2;
    }

    @Override
    protected boolean onDrawSelected(Canvas canvas, Calendar calendar, int x, int y, boolean hasScheme,
                                     boolean isSelectedPre, boolean isSelectedNext) {
        int cx = x + mItemWidth / 2;
        int cy = y + mItemHeight / 2;
        if (isSelectedPre) {
            if (isSelectedNext) {
                mSelectedPaint.setColor(new Color(0x1f59CEF3));
                canvas.drawRect(x, cy - mRadius, x + mItemWidth, cy + mRadius, mSelectedPaint);
            } else {//最后一个，the last
                mSelectedPaint.setColor(new Color(0x1f59CEF3));
                canvas.drawRect(x, cy - mRadius, cx, cy + mRadius, mSelectedPaint);
                mSelectedPaint.setColor(new Color(0xff59CEF3));
                canvas.drawCircle(cx, cy, mRadius, mSelectedPaint);
            }
        } else {
            if (isSelectedNext) {
                mSelectedPaint.setColor(new Color(0x1f59CEF3));
                canvas.drawRect(cx, cy - mRadius, x + mItemWidth, cy + mRadius, mSelectedPaint);
            }
            mSelectedPaint.setColor(new Color(0xff59CEF3));
            canvas.drawCircle(cx, cy, mRadius, mSelectedPaint);
        }
        return true;
    }

    @Override
    protected void onDrawScheme(Canvas canvas, Calendar calendar, int x, int y, boolean isSelected) {
        mSchemePaint.setColor(new Color(0xff59CEF3));

        int radius = AttrHelper.vp2px(1.5f, getContext());
        canvas.drawCircle(x + mItemWidth / 2f,
                y + (mItemHeight + getDayTextHeight()) / 2f + radius + AttrHelper.vp2px(2f, getContext()),
                radius, mSchemePaint);
    }


    @Override
    protected void onDrawText(Canvas canvas, Calendar calendar, int x, int y, boolean hasScheme, boolean isSelected) {
        float baselineY = mTextBaseLine + y;
        int cx = x + mItemWidth / 2;

        boolean isInRange = isInRange(calendar);
        boolean isEnable = !onCalendarIntercept(calendar);

//        if (hasScheme) {
//            String scheme = calendar.getScheme();
//            mSchemePaint.setTextSize(UIUtils.sp2px(getContext(),7));
//            mSchemePaint.getTextBounds(scheme, 0, scheme.length(), mRect);
//            Paint.FontMetrics metrics = mSchemePaint.getFontMetrics();
//            //baseline
//            float v = metrics.descent + (metrics.bottom - metrics.top) / 2;
//            canvas.drawText(scheme, cx - mRect.width() / 2f,
//                y + mItemHeight / 2f + getDayTextHeight() / 2 + UIUtils.dp2px(getContext(),7f) + v,
//                mSchemePaint);
//        }

        if (isSelected) {
            int index = mItems.indexOf(calendar);
            boolean pre = isSelectPreCalendar(calendar, index);
            boolean next = isSelectNextCalendar(calendar, index);
            mSelectTextPaint.setColor(new Color(pre && next ? 0xff4A4A4A : Color.WHITE.getValue()));
            canvas.drawText(mSelectTextPaint, String.valueOf(calendar.getDay()),
                    cx,
                    baselineY);
        } else {
            canvas.drawText(
                    calendar.isCurrentDay() ? mCurDayTextPaint :
                            calendar.isCurrentMonth() && isInRange && isEnable ? mCurMonthTextPaint : mOtherMonthTextPaint,
                    String.valueOf(calendar.getDay()), cx, baselineY);
        }
    }

    private float getDayTextHeight() {
        Paint.FontMetrics metrics = mCurMonthTextPaint.getFontMetrics();
        return metrics.descent - metrics.ascent;
    }
}
